package LCR;

public class isBalanced {
    int left = 0;
    int right = 0;
    public boolean isBalanced(TreeNode root) {
        if(root.left == null && root.right == null) {
            return true;
        }

        if(left - right > 1 || left - right < -1){
            return false;
        }
        left = heightTree(root.left,0);
        right = heightTree(root.right,0);
        isBalanced(root.left);
        isBalanced(root.right);
        return true;


    }

    public static int heightTree(TreeNode root, int h) {
        if (root.left == null && root.right == null) {
            return h;
        }

        if (root.left != null && root.right == null) {
            heightTree(root.left, h + 1);
        }
        if (root.left == null && root.right != null) {
            heightTree(root.right, h + 1);

        }
        heightTree(root.left,h+1);
        heightTree(root.right,h+1);
        return h;
    }
}
